<?php

class Application_Model_DbTable_DbGlobal extends Zend_Db_Table_Abstract
{
    // set name value
	public function setName($name){
		$this->_name=$name;
	}
	
	/**
	 * get selected record of $sql
	 * @param string $sql
	 * @return array $row;
	 */
	public function getGlobalDb($sql)
  	{
  		$db=$this->getAdapter();
  		$row=$db->fetchAll($sql);
  		if(!$row) return NULL;
  		return $row;
  	}
  	public function getGlobalDbRow($sql)
  	{
  		$db=$this->getAdapter();
  		$row=$db->fetchRow($sql);
  		if(!$row) return NULL;
  		return $row;
  	}
  	public function addteacher($arr)
  	{
  		 $_name = 'tbl_teacher';
  		//print_r($arr);exit();
  		$array_data = array(
  				"id"	=>	$arr["id_code"],
  				"dpt_id"	=>	$arr[""],
  				"teacher_name"	=>	$arr["teacher_name"],
  				"gender"	=>	$arr["gender"],
  				"pob"	=>	$arr["pob"],
  				"dob"	=>	$arr["dob"],
  				"tel" =>$arr["tel"],
  				"email" =>$arr["email"],
  				"password" =>$arr[""],
  				"education_level" =>$arr["education_level"],
  				"finish_from" =>$arr["finish_from"],
  				"year" =>$arr["year"],
  				"description" =>$arr["description"],
  				"status" =>$arr["status"],
  		);
  		$id=$this->insert($array_data);
  	
  	
  	}
  	public static function getActionAccess($action)
    {
    	$arr=explode('-', $action);
    	return $arr[0];    	
    }     
    public function addRecord($data,$tbl_name){
    	$this->setName($tbl_name);
    	return $this->insert($data);
    }
    
    
    /**
     * update record to table $tbl_name
     * @param array $data
     * @param int $id
     * @param string $tbl_name
     */
//     public function updateRecord($data,$id,$tbl_name){
//     	$this->setName($tbl_name);
//     	$where=$this->getAdapter()->quoteInto('pro_id=?',$id);
//     	$this->update($data,$where);
//     }
	public function updateRecord($data,$id,$updateby,$tbl_name){
		$this->setName($tbl_name);
		$where=$this->getAdapter()->quoteInto($updateby.'=?',$id);
		$this->update($data,$where);
	}
    
    public function DeleteRecord($tbl_name,$id){
    	$db = $this->getAdapter();
		$sql = "UPDATE ".$tbl_name." SET status=0 WHERE id=".$id;
		return $db->query($sql);
    }

    public function deleteRecords($sql){
    	$db = $this->getAdapter();
		return $db->query($sql);
    } 

     public function DeleteData($tbl_name,$where){
    	$db = $this->getAdapter();
		$sql = "DELETE FROM ".$tbl_name.$where;
		return $db->query($sql);
    } 
    
    public function convertStringToDate($date, $format = "Y-m-d H:i:s")
    {
    	if(empty($date)) return NULL;
    	$time = strtotime($date);
    	return date($format, $time);
    }
    /* @Desc: add or sub qty of item depend on item and stock
     * @param $stockID stock id
     * @param $itemQtys array of item id and item qty
     * @param $sign: + | -
     * */
    public function query($sql){
    	$db = $this->getAdapter();
    	return $db->query($sql);	
    }
    public function fetchArray($result){
    	$db = $this->getAdapter();
    	return mysql_fetch_assoc($result);
    }
    public function getAccessPermission($level, $str_condition,  $location_id){
    	if($level==1 OR $level==2){
    		return false;
    	}
    	else{
    		$result = "$str_condition =".$location_id;
    		return $result;
    	} 
    }
    
    public function getLocation(){
    	$sql ="SELECT * FROM tbwu_location WHERE 1";
    	return $this->getGlobalDb($sql);
    }
    public function getLocationByID($id){
    	$sql ="SELECT * FROM tbwu_location WHERE location_id=$id";
    	return $this->getGlobalDbRow($sql);
    }
}
?>